﻿Imports System.Configuration
Imports System.ServiceProcess
Imports QueueManager.Service.Config
Imports QueueManager.Helper

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class QueueService
    Inherits System.ServiceProcess.ServiceBase

    'UserService overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main(ByVal args() As String)

        Dim ServicesToRun() As System.ServiceProcess.ServiceBase
        Dim objMonitor As QueueMonitor
        Dim objSettings As QueueSection
        Dim objSettingsForm As Settings

        'Run once if in console mode
        If Environment.UserInteractive Then

            LogHelper.Output("CRM Queue Manager Service version " & My.Application.Info.Version.ToString, False)
            LogHelper.Output("", False)

            If My.Application.CommandLineArgs.Contains("-settings") Then
                LogHelper.Output("Launching settings dialog...", False)
                objSettingsForm = New Settings
                objSettingsForm.ShowDialog()
                Exit Sub
            End If
            
            Try
                objSettings = CType(ConfigurationManager.GetSection("queues"), QueueSection)
            Catch ex As Exception
                LogHelper.Log("Configuration error.", ex, LogHelper.LogType.Exception)
                Exit Sub
            End Try

            objMonitor = New QueueMonitor

            LogHelper.Output("Processing queues")

            'Loop through the queues and process them according to their settings
            For Each objItem As QueueSetting In objSettings.Queues
                objMonitor.ProcessQueue(objItem)
                objMonitor.ProcessTempQueue(objItem)
            Next

            LogHelper.Output("", False)
            LogHelper.Output("Finished.")

        Else
            'Run as service
            ServicesToRun = New System.ServiceProcess.ServiceBase() {New QueueService}
            System.ServiceProcess.ServiceBase.Run(ServicesToRun)
        End If

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  
    ' Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        '
        'QueueService
        '
        Me.AutoLog = False
        Me.CanPauseAndContinue = True
        Me.CanShutdown = True
        Me.ServiceName = "CRMQueueService"

    End Sub

End Class
